/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;

import entity.Product;
import entity.User2;
import java.util.ArrayList;
import javax.ejb.Stateful;

/**
 *
 * @author lab1
 */
@Stateful
public class Cart implements CartRemote {
    //List chua Cart
    private ArrayList<Product> listcart;
    //Them Product vao Cart
    public void addToCart(String proID, String proName, double price, int quantity) {
        Product pro = new Product();
        pro.setProductID(proID);
        pro.setProductName(proName);
        pro.setPrice(price);
        pro.setQuantity(quantity);
        if (listcart == null) {
            listcart = new ArrayList<Product>();
        }
        boolean check = false;
        for (int i = 0; i < listcart.size(); i++) {
            if (listcart.get(i).getProductID().equals(proID)) {
                listcart.get(i).setQuantity(listcart.get(i).getQuantity() + 1);
                check = true;
            }
        }
        if (!check) {
            listcart.add(pro);
        }
    }
    //Lay ra tong tien cua Cart
    public double getTotalAmount() {
        double total = 0;
        if (listcart == null || listcart.size() == 0) {
            return total;
        }
        for (Product item : listcart) {
            total += item.getMoney();
        }
        return total;
    }
    //tra lai ListCart
    public ArrayList<Product> getListcart() {
        return listcart;
    }
    //Xoa  Product trong Cart
    public void removeProduct(String proID) {
        boolean check = false;
        int inde = 0;
        for (int i = 0; i < listcart.size(); i++) {
            if (!check) {
                inde++;
            }
            if (listcart.get(i).getProductID().equals(proID)) {
                check = true;
            }
        }
        if (inde != 0) {
            listcart.remove(inde - 1);
        }
    }
    //Cap nhat lai Cart khi Employee Edit Cart
    public void resetProduct(ArrayList<Product> listPro) {
        listcart = listPro;
    }
    private User2 use;
    //Lay ra User khi Employee Login thanh cong
    public User2 ShowUser() {
        if (use == null) {
            use = new User2();
        }
        return use;
    }
    //cap nhat User khi login thanh congf
    public void updateUse(User2 use) {
        this.use = use;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
